%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Paper: The Technical Default Spread, by Emilio Bisetti, Kai Li, and Jun Yu
% This code is used to produce Figure 2 in the paper
% Auhtor: Emilio Bisetti, Kai Li, and Jun Yu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Adjust path to folder where replication file is stored
clear;
clc;

cd([cd '/Model without Risk Shocks']);

% before you run following code, you need to run two dyare files:
%    1) mainmodel_tfpunc_nocov in folder GE Model computation nocovenant
%    2) mainmodel_tfpunc in folder GE Model computation with UNC shocks

% For dynare package, you need to go to https://www.dynare.org/, download
% the package and install it on your computer. We are using Dynare 4.5.3
% for our model computation. 


cd([cd '/GE Model computation with covenant']);

% For the covenant model
dynare mainmodel_tfpunc;
clear

% No covenant model
cd ..
cd([cd '/GE Model computation nocovenant']);

dynare mainmodel_tfpunc_nocov;
clear
cd ..

%% Load data for ploting

cd([cd '/GE Model computation with covenant']);

load mainmodel_tfpunc_results.mat
IRN_A = oo_.irfs.a_ea(1,1:20)*100;
IRN_Y = oo_.irfs.y_ea(1,1:20)*100;
IRN_I = oo_.irfs.ii_ea(1,1:20)*100;
IRN_C = oo_.irfs.c_ea(1,1:20)*100;
IRN_Q = oo_.irfs.q_ea(1,1:20) * 100;
NN=size(IRN_A,2);
IRN_dY = oo_.irfs.dy_ea(1,1:20)*100;
IRN_dI = oo_.irfs.di_ea(1,1:20)*100;
IRN_dC = oo_.irfs.dc_ea(1,1:20)*100;
IRN_dk = oo_.irfs.dk_ea(1,1:20) * 100;


IRN_N = oo_.irfs.n_ea(1,1:20)*100;
IRN_B = oo_.irfs.b_ea(1,1:20)*100;
IRN_H = oo_.irfs.h_ea(1,1:20)*100;
IRN_premium = oo_.irfs.premiumH_ea(1,1:20) * 4*100; 

IRN_act_def = oo_.irfs.act_def_ea(1,1:20)*100;
IRN_td_def = oo_.irfs.TDprob_ea(1,1:20)*100;
IRN_dtd_def = oo_.irfs.dTDprob_ea(1,1:20)*100;
IRN_spread = oo_.irfs.spread_ea(1,1:20)*100;
IRN_OMEGA = oo_.irfs.omega0bar_ea(1,1:20)*100;

IRN_K = cumsum(IRN_dk);
IRN_YA =IRN_Y+ IRN_K;
IRN_IA =IRN_I+ IRN_K;
IRN_CA =IRN_C+ IRN_K;
IRN_NA =IRN_N+ IRN_K;
IRN_BA =IRN_B+ IRN_K;

X=1:NN;

%% No covenant model
cd ..
cd([cd '/GE Model computation nocovenant']);

load mainmodel_tfpunc_nocov_results.mat
IRNCOV_A = oo_.irfs.a_ea(1,1:20)*100;
IRNCOV_Y = oo_.irfs.y_ea(1,1:20)*100;
IRNCOV_I = oo_.irfs.ii_ea(1,1:20)*100;
IRNCOV_C = oo_.irfs.c_ea(1,1:20)*100;
IRNCOV_Q = oo_.irfs.q_ea(1,1:20) * 100;
IRNCOV_dY = oo_.irfs.dy_ea(1,1:20)*100;
IRNCOV_dI = oo_.irfs.di_ea(1,1:20)*100;
IRNCOV_dC = oo_.irfs.dc_ea(1,1:20)*100;
IRNCOV_dk = oo_.irfs.dk_ea(1,1:20) * 100;
IRNCOV_RK = oo_.irfs.rk_ea(1,1:20) * 100;
IRNCOV_N = oo_.irfs.n_ea(1,1:20)*100;
IRNCOV_B = oo_.irfs.b_ea(1,1:20)*100;
IRNCOV_H = oo_.irfs.h_ea(1,1:20)*100;
IRNCOV_premium = oo_.irfs.premiumH_ea(1,1:20) * 4*100; 
IRNCOV_act_def = oo_.irfs.act_def_ea(1,1:20)*100;

IRNCOV_spread = oo_.irfs.spread_ea(1,1:20)*100;


cd ..

%% Produce the final figure
X=0:1:NN;
h=figure('Name','fig1');
AA=get(gca,'ColorOrder');

subplot(3,3,1)
plot(X', [0,IRN_A]','k--',X', [0,IRNCOV_A]','k:','LineWidth',2);
title('TFP (Z), %','FontSize',10);
legend('Benchmark','No Covenant');

subplot(3,3,2)
plot(X', [0,IRN_I]','k--',X', [0,IRNCOV_I]','k:','LineWidth',2);
title('Investment rate (I/K), %','FontSize',10);

subplot(3,3,3)
plot(X', [0,IRN_Q]','k--',X', [0,IRNCOV_Q]','k:','LineWidth',2);
title('Capital Price, %','FontSize',10);

subplot(3,3,4)
plot(X',[0,IRN_N]','k--',X', [0,IRNCOV_N]','k:','LineWidth',2);
title('Net Worth-to-Captial, %','FontSize',10);


subplot(3,3,5)
plot(X', [0,IRN_H]','k--',X', [0,IRNCOV_H]','k:','LineWidth',2);
title('Debt-to-Net Worth, %','FontSize',10);


subplot(3,3,6)
plot(X', [0,IRN_premium]','k--',X', [0,IRNCOV_premium]','k:','LineWidth',2);
title('External Finance Premium, %','FontSize',10);


subplot(3,3,7)
plot(X', [0,IRN_dtd_def]','k--','LineWidth',2);
ylim([min(IRN_dtd_def)-0.005 max(IRN_dtd_def)]);
title('Changes in Technical Default Probability, %','FontSize',10);


subplot(3,3,8)
plot(X', [0,IRN_act_def]','k--',X', [0,IRNCOV_act_def]','k:','LineWidth',2);
title('Actual Default Probability, %','FontSize',10);


subplot(3,3,9)
plot(X', [0,IRN_spread]','k--',X', [0,IRNCOV_spread]','k:','LineWidth',2);
title('Loan Spread, %','FontSize',10);


saveas(h,'..\output\tfp_nounc.eps','epsc');